﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EzDesk.DesktopClient;
using EzDesk.DesktopClient.ServiceLocator.ClientService;
using EzDesk.DesktopClient.Model;
using EzDesk.DesktopClient.Event;
using EzDesk.EventAggregation;
using EzDesk.Generic;
using System.Runtime.InteropServices;
using System.Threading;

namespace EzDesk.DesktopClient
{
    public class UserManagement : ManagementBase<UserManagement>
    {
        private LoginEvent _loginEvent = EventAggregator<LoginEvent>.GetEvent();
        private UserProfile _logUser;
        private UserState _logUserState;

        private string _userToken;

        public string UserToken
        {
            get
            {
                if (!_logUserState.IsAvailable)
                {
                    _userToken = null;
                }
                return _userToken;

            }
            set
            {
                _userToken = value;
                InternetSetCookie(ServiceConfigurationManagement.Instance.AppPortalLocation, null, string.Format("{0}={1};", ServiceConfigurationManagement.Instance.UserTokenCookieName, _userToken));
            }
        }

        public UserProfile LogUser
        {
            get
            {
                if (!LogUserState.IsAvailable) return null;
                return _logUser;
            }
            set { _logUser = value; }
        }
        public UserState LogUserState
        {
            get
            {
                if (_logUserState == null)
                {
                    _logUserState = new UserState();
                }
                return _logUserState;
            }
            set { _logUserState = value; }
        }

        public UserManagement()
            : base()
        {
        }

        private void SetClientData(UserProfile user, UserState userState)
        {
            this.LogUser = user;
            this.LogUserState = userState;
        }


        public void Login(string userName, string pwd)
        {
            ThreadPool.QueueUserWorkItem(
                callback =>
                {
                    string userToken = this.ClientService.Login(userName, pwd);
                    if (userToken == null)
                    {
                        LoginFaild();
                    }
                    else
                    {
                        LoginSuccess(userName, userToken);
                    }
                });
        }

        private void LoginSuccess(string userName, string userToken)
        {
            this.UserToken = userToken;
            InitUserInfo(userName);
            PublishLoginSuccessMessage();
        }


        private void InitUserInfo(string userName)
        {
            UserManagement.Instance.LogUser = this.ClientService.GetUserProfileByUserName(userName);
            UserManagement.Instance.LogUserState.IsAvailable = true;
        }

        private void PublishLoginSuccessMessage()
        {
            LoginArgs args = new LoginArgs
            {
                IsSuccess = true,
                Message = "Success!"
            };
            _loginEvent.Publish(args);
        }

        private void LoginFaild()
        {
            LoginArgs args = new LoginArgs { IsSuccess = false, Message = "Failed!" };
            _loginEvent.Publish(args);
        }

        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
    }
}
